package cs3343.project.family;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;


public class CardController {
	
	private GameController gc;
	public static HashMap<String, EventCard> eventCards = new HashMap<String, EventCard>();
	public static HashMap<String, OralCard> oralCards = new HashMap<String, OralCard>();
	public static HashMap<String, ActionCard> actionCards = new HashMap<String, ActionCard>();
	public static HashMap<String, MagicToolCard> magicToolCards = new HashMap<String, MagicToolCard>();
	
	public CardController() {
		
	}
	
	public CardController(GameController gc) {
		this.gc = gc;
	}
	
	//TODO int parameter rather than string
	/*
	public static Card drawCard(String cardType) {
		int ranCard = 0;
		switch (cardType) {
			case "event":
				ranCard = (int)(CardController.eventCards.size() * Math.random() + 1);
				return eventCards.get(String.valueOf(ranCard));
			case "tool":
				ranCard = (int)(CardController.magicToolCards.size() * Math.random() + 1);
				return magicToolCards.get(String.valueOf(ranCard));
			case "action":
				ranCard = (int)(CardController.actionCards.size() * Math.random() + 1);
				return actionCards.get(String.valueOf(ranCard));
			case "oral":
				ranCard = (int)(CardController.oralCards.size() * Math.random() + 1);
				return oralCards.get(String.valueOf(ranCard));
			default:
				return null;
		}
	}*/
	
	public static Card drawCard(int cardType) {
		int ranCard = 0;
		switch (cardType) {
			case 1:
				ranCard = (int)(CardController.eventCards.size() * Math.random() + 1);
				return eventCards.get(String.valueOf(ranCard));
			case 2:
				ranCard = (int)(CardController.magicToolCards.size() * Math.random() + 1);
				return magicToolCards.get(String.valueOf(ranCard));
			case 3:
				ranCard = (int)(CardController.actionCards.size() * Math.random() + 1);
				return actionCards.get(String.valueOf(ranCard));
			case 4:
				ranCard = (int)(CardController.oralCards.size() * Math.random() + 1);
				return oralCards.get(String.valueOf(ranCard));
			default:
				return null;
		}
	}
	
	public static void importAllCard() {
		importEventCard();
		importToolCard();
		importActionCard();
		importOralCard();
	}
	
	public static void importEventCard() {
		String result[];
		try 
	    {
	        BufferedReader infile = new BufferedReader(new InputStreamReader(
	        		new FileInputStream(ConfigManager.EVENTCARD_PATH), "UTF-8"));
	        String str;
	        while ((str = infile.readLine()) != null) 
	        {
	        	if (str.length() > 0) {
                    // UTF - 8 BOM CHAR
                    int c = str.charAt(0);
                    if (c == 65279)
                    	str = str.substring(1, str.length());
	        	}
	            result = str.split("\t");
	            
	            EventCard event = new EventCard(result[1], result[2], result[3], result[4], result[5], result[6], result[7], result[8], result[9]);
	            eventCards.put(result[0], event);
	        }
	        infile.close();
	    } 
	    catch (IOException e) 
	    {
	    	System.out.println(e.toString()); // java.io.FileNotFoundException
	        // Exceptions ignored.
	    }
	}
	
	public static void importOralCard() {
		String result[];
		try 
	    {
	        BufferedReader infile = new BufferedReader(new InputStreamReader(
	        		new FileInputStream(ConfigManager.ORALCARD_PATH), "UTF-8"));
	        String str;
	        while ((str = infile.readLine()) != null) 
	        {
	        	if (str.length() > 0) {
                    // UTF - 8 BOM CHAR
                    int c = str.charAt(0);
                    if (c == 65279)
                    	str = str.substring(1, str.length());
	        	}
	            result = str.split("\t");
	            
	            OralCard oral = new OralCard(result[1]);

	            oralCards.put(result[0], oral);
	        }
	        infile.close();
	    } 
	    catch (IOException e) 
	    {
	    	System.out.println(e.toString()); // java.io.FileNotFoundException
	        // Exceptions ignored.
	    }
	}
	
	public static void importActionCard() {
		String result[];
		try 
	    {
	        BufferedReader infile = new BufferedReader(new InputStreamReader(
	        		new FileInputStream(ConfigManager.ACTIONCARD_PATH), "UTF-8"));
	        String str;
	        while ((str = infile.readLine()) != null) 
	        {
	        	if (str.length() > 0) {
                    // UTF - 8 BOM CHAR
                    int c = str.charAt(0);
                    if (c == 65279)
                    	str = str.substring(1, str.length());
	        	}
	            result = str.split("\t");
	            
	            ActionCard action = new ActionCard(result[1]);

	            actionCards.put(result[0], action);
	        }
	        infile.close();
	    } 
	    catch (IOException e) 
	    {
	    	System.out.println(e.toString()); // java.io.FileNotFoundException
	        // Exceptions ignored.
	    }
	}
	
	public static void importToolCard() {
		String result[];
		try 
	    {
	        BufferedReader infile = new BufferedReader(new InputStreamReader(
	        		new FileInputStream(ConfigManager.TOOLCARD_PATH), "UTF-8"));
	        String str;
	        while ((str = infile.readLine()) != null) 
	        {
	        	if (str.length() > 0) {
                    // UTF - 8 BOM CHAR
                    int c = str.charAt(0);
                    if (c == 65279)
                    	str = str.substring(1, str.length());
	        	}
	            result = str.split("\t");
	            
	            MagicToolCard tool = new MagicToolCard(result[1]);

	            magicToolCards.put(result[0], tool);
	        }
	        infile.close();
	    } 
	    catch (IOException e) 
	    {
	    	System.out.println(e.toString()); // java.io.FileNotFoundException
	        // Exceptions ignored.
	    }
	}
	
}
